//
//  ServiceCategoryViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/29.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "ZZLayout.h"
#import "XLPageViewController.h"

#import "ServiceCategoryListCell.h"

#import "ServiceCategorySectionHeader.h"

#import "SecondaryClassificationView.h"
#import "ServiceCatSubSegViewController.h"

#import "ServiceCategoryViewController.h"

@interface ServiceCategoryViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, ZZLayoutDelegate>

@property (nonatomic, strong) SecondaryClassificationView *secondaryClassificationView;

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSMutableArray <ProClassificationModel *>*classificationDataArray;

@property (nonatomic, assign) NSInteger firIndex;

@end

@implementation ServiceCategoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initView];
    [self request_catData];
//    [self prepareData];
}

#pragma mark initView
- (void)initView {
    
    self.firIndex = 0;
    
    self.title = @"全部分类";

    [self.view addSubview:self.secondaryClassificationView];
    [self.view addSubview:self.collectionView];
    
    [self.secondaryClassificationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.width.mas_equalTo(120);
    }];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.secondaryClassificationView.mas_right);
        make.top.mas_equalTo(0);
        make.right.bottom.mas_equalTo(0);
    }];
    
    [self initViewAction];
}

- (void)initViewAction {
    WEAKSELF
    self.secondaryClassificationView.SecondaryClassificationViewBlock = ^(NSInteger index) {
        [weakSelf secClassificationSelectIndex:index];
    };
}

- (void)secClassificationSelectIndex:(NSInteger)index {
    
    for (ProClassificationModel *secModel in self.classificationDataArray) {
        secModel.isSelect = NO;
    }
    self.firIndex = index;
    self.classificationDataArray[self.firIndex].isSelect = YES;
    [self.secondaryClassificationView fillWithArray:self.classificationDataArray];
    [self.collectionView reloadData];
}

#pragma mark- 协议方法
//collectionView的协议方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.classificationDataArray[self.firIndex].sysClassificationList.count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    ServiceCatSubSegViewController *vc = [[ServiceCatSubSegViewController alloc] init];
    vc.model = self.classificationDataArray[self.firIndex];
    vc.selectIndex = indexPath.row;
    [self.navigationController pushViewController:vc animated:YES];
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionReusableView *reusableView = nil;
    // 区头
    if (kind == UICollectionElementKindSectionHeader) {
        ServiceCategorySectionHeader *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ServiceCategorySectionHeader" forIndexPath:indexPath];
//        [headerView fillHeadWithModel:self.classificationDataArray[0].childs[0]];
        
//        ProClassificationModel * headerModel = self.classificationDataArray[self.firIndex].sysClassificationList[indexPath.section];
        headerView.mainTitleLab.text = @"套餐分类";
        
        reusableView = headerView;
    }
    // 区尾
    if (kind == UICollectionElementKindSectionFooter) {
        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"UICollectionElementKindSectionFooter" forIndexPath:indexPath];
        reusableView = footerView;
    }
    return reusableView;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ServiceCategoryListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ServiceCategoryListCellID" forIndexPath:indexPath];
    
    if (indexPath.section%2>0) {
        cell.backgroundColor = UIColorHex(0xf1f1f1);
    } else {
        cell.backgroundColor = UIColorHex(0xfff3f9);
    }
    
    [cell fillCellWithModel:self.classificationDataArray[self.firIndex].sysClassificationList[indexPath.row]];
    
    return cell;
}

//ZZLyout的流协议方法
- (CGFloat)layout:(ZZLayout *)layout heightForRowAtIndexPath:(NSIndexPath *)indexPath {//返回item的高
    return 32;
}

- (NSInteger)layout:(ZZLayout *)layout columnNumberAtSection:(NSInteger)section {//每个区有几列
    return 2;
}

- (UIEdgeInsets)layout:(ZZLayout *)layout insetForSectionAtIndex:(NSInteger)section {//设置每个区的边距
    
    return UIEdgeInsetsMake(10, 25, 25, 25);
}

- (NSInteger)layout:(ZZLayout *)layout lineSpacingForSectionAtIndex:(NSInteger)section {//设置每个区的行间距

    return 20;
}

- (CGFloat)layout:(ZZLayout*)layout interitemSpacingForSectionAtIndex:(NSInteger)section {//设置每个区的列间距
    return 20;
}

- (CGSize)layout:(ZZLayout *)layout referenceSizeForHeaderInSection:(NSInteger)section {//设置区头的高度
    return CGSizeMake(TTScreenWidth, 50);
}

- (CGSize)layout:(ZZLayout *)layout referenceSizeForFooterInSection:(NSInteger)section {//设置区尾的高度
    return CGSizeMake(TTScreenWidth, CGFLOAT_MIN);
}

- (CGFloat)layout:(ZZLayout*)layout spacingWithLastSectionForSectionAtIndex:(NSInteger)section {//本区区头和上个区区尾的间距
    return CGFLOAT_MIN;
}

- (UIColor *)layout:(UICollectionView *)layout colorForSection:(NSInteger)section {//设置不同分区的不同背景颜色
    return [UIColor whiteColor];
}



#pragma mark- 懒加载
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        
        ZZLayout *layout = [[ZZLayout alloc] initWith:ZZLayoutFlowTypeVertical delegate:self];
        //打开区头悬浮功能(默认关闭)
        layout.sectionHeadersPinToVisibleBounds = YES;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = UIColorHex(0xffffff);
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        [_collectionView registerNib:[UINib nibWithNibName:@"ServiceCategoryListCell" bundle:nil] forCellWithReuseIdentifier:@"ServiceCategoryListCellID"];

        
        [_collectionView registerNib:[UINib nibWithNibName:@"ServiceCategorySectionHeader" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ServiceCategorySectionHeader"];
        
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"UICollectionElementKindSectionFooter"];
        
        
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;

        _collectionView.bounces = YES;
        self.edgesForExtendedLayout = UIRectEdgeNone;
        if (@available(iOS 11.0, *)) {
            _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        
    }
    return _collectionView;
}

- (SecondaryClassificationView *)secondaryClassificationView {
    if (!_secondaryClassificationView) {
        _secondaryClassificationView = [[NSBundle mainBundle] loadNibNamed:@"SecondaryClassificationView"owner:nil options:nil].firstObject;
    }
    return _secondaryClassificationView;
}


- (void)request_catData {
    NSMutableDictionary *dic = @{}.mutableCopy;

    [dic setValue:@"1" forKey:@"scType"];
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_Category];
    
    [SVProgressHUD showWithStatus:@"正在加载"];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        [SVProgressHUD dismiss];
        if ([data isKindOfClass:[NSDictionary class]]) {

            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                NSDictionary *resultDic = [data valueForKey:@"data"];
                if (![resultDic isKindOfClass:[NSDictionary class]]) {
                    return;
                }
                NSArray *oneList = [resultDic valueForKey:@"sysClassificationList"];
                if ([oneList isKindOfClass:[NSArray class]]) {
                    
                    self.classificationDataArray = @[].mutableCopy;
                    for (int i = 0; i < oneList.count; i++) {
                        ProClassificationModel *oneModel = [ProClassificationModel modelWithDictionary:oneList[i]];

                        if (i==0) {
                            oneModel.isSelect = YES;
                        }
                        
                        NSArray *twoList = [oneList[i] valueForKey:@"sysClassificationList"];
                        oneModel.sysClassificationList = @[].mutableCopy;
                        if ([twoList isKindOfClass:[NSArray class]]) {
                            
                            for (int j = 0; j < twoList.count; j++) {
                                ProClassificationModel *twoModel = [ProClassificationModel modelWithDictionary:twoList[j]];
                                NSArray *threeList = [twoList[j] valueForKey:@"sysClassificationList"];
                                twoModel.sysClassificationList = @[].mutableCopy;
                                
                                if ([threeList isKindOfClass:[NSArray class]]) {
                                    for (int k = 0; k < threeList.count; k++) {
                                        ProClassificationModel *threeModel = [ProClassificationModel modelWithDictionary:threeList[k]];
                                        [twoModel.sysClassificationList addObject:threeModel];
                                    }
                                }
                                [oneModel.sysClassificationList addObject:twoModel];
                            }
                        }
                        [self.classificationDataArray addObject:oneModel];
                    }
                    [self.secondaryClassificationView fillWithArray:self.classificationDataArray];
                    [self.collectionView reloadData];
                }
            }
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        
        [SVProgressHUD dismiss];
    }];
}


@end
